package com.leetcode.Offer;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

public class JZ55_I {
    //递归遍历:时间快，内存大
    int maxSize;

    public int maxDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        dfs(root, 1);
        return maxSize;

    }

    private void dfs(TreeNode root, int deep) {

        maxSize = Math.max(maxSize, deep);
        if (root.left != null) {
            dfs(root.left, deep + 1);
        }
        if (root.right != null) {
            dfs(root.right, deep + 1);
        }

    }

    //层次遍历：时间大，内存小
    public int maxDepth2(TreeNode root) {
        //层次遍历
        if (root == null) {
            return 0;
        }
        int deep = 0;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                TreeNode poll = queue.poll();
                if (poll.left != null) {
                    queue.add(poll.left);
                }
                if (poll.right != null) {
                    queue.add(poll.right);
                }
            }
            deep++;
        }
        return deep;
    }

}
